package com.tjd.uploaddemo.utils.fileutils;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.InputStream;

/**
 * @Auther: 田金东
 * @Date: 12/12/0012
 **/
public class FileChunk implements Comparable<FileChunk> {
    /**
     * 文件的全限定名称
     */
    private final String basePath;
    /**
     * 排序的ID
     */
    private final int sortId;
    /**
     * 文件上传的时间戳
     */
    private final String timestamp;
    /**
     * 文件上传的sessionId
     */
    private final String sessionId;


    public FileChunk(String basePath, int sortId, String timestamp, String sessionId) {
        this.basePath = basePath;
        this.sortId = sortId;
        this.timestamp = timestamp;
        this.sessionId = sessionId;
    }

    @Override
    public int compareTo(FileChunk o) {
        return this.sortId - o.sortId;
    }

    public String getBasePath() {
        return basePath;
    }

    public InputStream getInputStream() throws FileNotFoundException {
        return new FileInputStream(this.basePath);
    }

    public boolean deleteFile() {
        File file = new File(basePath);
        if (file.exists() && file.isFile()) {
            return file.delete();
        }
        return false;
    }
}
